local skel = fk.CreateSkill {
  name = "emo__chenjie",
}

Fk:loadTranslationTable{
  ["emo__chenjie"] = "谶劫",
  [":emo__chenjie"] = "回合开始时，你可以预测直到下回合开始时，首个造成和受到伤害的角色，每猜中一个，你卜算2并摸两张牌。首次发动后，你可令一名角色视为对你指定的角色使用【决斗】。",

  ["@[emo__chenjie]"] = "谶劫",
  ["#emo__chenjie-ask"] = "谶劫：你可以预测直到下回合开始时，首个造成和受到伤害的角色",
  ["#emo__chenjie-from"] = "谶劫：预测首个<font color='red'>造成伤害</font>的角色",
  ["#emo__chenjie-to"] = "谶劫：预测首个<font color='green'>受到伤害</font>的角色",
  ["#emo__chenjie-duel"] = "谶劫：你可选择两名角色，令前者对后者使用【决斗】",
  ["@emo__chenjie_user"] = "使用者",
  ["@emo__chenjie_target"] = "目标",
  ["emo__chenjie_select"] = "谶劫",
}

Fk:addQmlMark{
  name = "emo__chenjie",
  qml_path = "",
  how_to_show = function(name, value, p)
    if type(value) ~= "table" then return " " end
    local from, to = value.from, value.to
    if type(from) == "number" then
      if Self == p then
        from = Fk:translate(Fk:currentRoom():getPlayerById(from).general, "zh_CN")
      else
        from = "?"
      end
    end
    if type(to) == "number" then
      if Self == p then
        to = Fk:translate(Fk:currentRoom():getPlayerById(to).general, "zh_CN")
      else
        to = "?"
      end
    end
    return "<font color='red'>" .. from .."</font> <font color='green'>" .. to .. "</font>"
  end,
}

skel:addEffect(fk.TurnStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__chenjie-ask"}) then
      local from = room:askToChoosePlayers(player, {
        min_num = 1, max_num = 1, skill_name = skel.name, cancelable = false, no_indicate = true,
        targets = room.alive_players, prompt = "#emo__chenjie-from",
      })[1]
      local to = room:askToChoosePlayers(player, {
        min_num = 1, max_num = 1, skill_name = skel.name, cancelable = false, no_indicate = true,
        targets = room.alive_players, prompt = "#emo__chenjie-to",
      })[1]
      local tos = {from, to}
      event:setCostData(self, { tos = tos, no_indicate = true })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    if not tos or #tos ~= 2 then return end
    local from, to = tos[1], tos[2]
    room:setPlayerMark(player, "@[emo__chenjie]", {from  = from.id, to = to.id })
    if player:usedSkillTimes(skel.name, Player.HistoryGame) == 1 then
      local _, dat = room:askToUseActiveSkill(player, {skill_name = "emo__chenjie_select", prompt = "#emo__chenjie-duel"})
      if dat then
        room:useVirtualCard("duel", nil, dat.targets[1], dat.targets[2], skel.name)
      end
    end
  end,
})

skel:addEffect(fk.Damage, {
  is_delay_effect = true,
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return data.extra_data and data.extra_data[skel.name] and data.extra_data[skel.name][player]
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    for _ = 1, data.extra_data[skel.name][player] do
      if player.dead then break end
      room:askToGuanxing(player, {cards = room:getNCards(2) })
      player:drawCards(2, skel.name)
    end
  end,
})

skel:addEffect(fk.Damage, {
  can_refresh = function (self, event, target, player, data)
    local mark = player:getMark("@[emo__chenjie]")
    return type(mark) == "table" and (type(mark.from) == "number" or type(mark.to) == "number")
  end,
  on_refresh = function (self, event, target, player, data)
    local mark = player:getMark("@[emo__chenjie]")
    local from, to = mark.from, mark.to
    local draw = 0
    if data.from and type(from) == "number" then
      if data.from.id == from then
        draw = draw + 1
        from = "√"
      else
        from = "x"
      end
    end
    if type(to) == "number" then
      if data.to.id == to then
        draw = draw + 1
        to = "√"
      else
        to = "x"
      end
    end
    player.room:setPlayerMark(player, "@[emo__chenjie]", {from  = from, to = to })
    if draw > 0 then
      data.extra_data = data.extra_data or {}
      data.extra_data[skel.name] = data.extra_data[skel.name] or {}
      data.extra_data[skel.name][player] = draw
    end
  end,
})

skel:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("@[emo__chenjie]") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@[emo__chenjie]", 0)
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@[emo__chenjie]", 0)
end)

local skel2 = fk.CreateSkill {
  name = "emo__chenjie_select",
}

skel2:addEffect("active", {
  target_num = 2,
  card_num = 0,
  target_filter = function (self, player, to_select, selected)
    if #selected == 0 then return true end
    if #selected == 1 then
      local from = selected[1]
      local card = Fk:cloneCard("duel")
      card.skillName = skel.name
      return from:canUseTo(card, to_select)
    end
  end,
  target_tip = function (self, player, to_select, selected)
    if to_select == selected[1] then
      return "@emo__chenjie_user"
    elseif to_select == selected[2] then
      return "@emo__chenjie_target"
    end
  end,
})

return {skel, skel2}
